import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';

class WebPage extends StatefulWidget{
  String title;
  String url;

  WebPage({
    Key key,
    this.title,
    this.url
  }): super(key: key);

  @override
  State<StatefulWidget> createState() {
    return new WebPageState();
  }
}

class WebPageState extends State<WebPage> {
  bool loading = true;

  final webViewPligin = new FlutterWebviewPlugin();

   @override
   void initState(){
    super.initState();

    // 监听加载事件
     webViewPligin.onStateChanged.listen((state){

     });

     // 监听url改变事件
     webViewPligin.onUrlChanged.listen((url){
       setState(() {
         loading = false;
       });
     });
   }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    List<Widget> titleContent = [];
    titleContent.add(new Text(
      widget.title,
      style: new TextStyle(color: Colors.white),
    ));
    if (loading) {
      titleContent.add(new CupertinoActivityIndicator());
    }
    titleContent.add(new Container(width: 50,));
    return new WebviewScaffold(
        url: widget.url,
      appBar: new AppBar(
        title: new Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: titleContent,
        ),
        iconTheme: new IconThemeData(color: Colors.white),
      ),
      withZoom: true,
      withLocalStorage: true,
      withJavascript: true,
    );
  }
}